package com.zyasdk.controll;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;

import com.zhuoyian.dl.Download;
import com.zhuoyian.dl.listener.DownloadListener;
import com.zhuoyian.dl.util.DownloadParameter;
import com.zyasdk.enums.AdType;
import com.zyasdk.enums.TaskState;
import com.zyasdk.model.Products;
import com.zyasdk.util.ZYALogUtils;
import com.zyasdk.util.LocalUtils;
import com.zyasdk.util.StaticSetting;

@SuppressLint("DefaultLocale")
public class DLTask {
	private static boolean isDownload = false;
	private static List<Products> list = new ArrayList<Products>();
	private static TaskState taskState = TaskState.READY;

	public static void start(Context context) {
		switch (taskState) {
		case RUN:
			// 如果当期任务处于运行中则不进行操作
			break;
		case WAIT:
			// 如果当前任务处于等待中则唤醒任务
			synchronized (taskThread) {
				taskThread.notify();
			}
			break;
		case READY:
			// 如果当前任务处于就绪状态则启动任务
			list = LocalUtils.getLocalNotDLData(AdType.AD, context);
			if (list != null && list.size() > 0) {
				isDownload = true;
				run(context);
			} else {
				ZYALogUtils.e("not local data to task");
			}
			break;
		}
	}

	public static void stop() {
		ZYALogUtils.e("stop task");
		isDownload = false;
	}

	private static Thread taskThread;

	private static void run(final Context context) {
		taskThread = new Thread() {
			public synchronized void run() {
				try {
					for (Products pro : list) {
						if (isDownload) {
							taskState = TaskState.RUN;
							ZYALogUtils.e("task is run");
							String filepath = pro.getApkfile();
							if (!filepath.toLowerCase().startsWith("http:")) {
								filepath = StaticSetting.DOWNLOAD_SERVICEPATH
										+ filepath;
							}
							ZYALogUtils.e("current task:" + filepath);
							Download.serialDlPointFile(filepath, DownloadParameter.TYPE_DEFAULT, pro, new DownloadListener() {
								
								@Override
								public void onDownload(DownloadParameter parameter, Object args) {
									switch (parameter.getDownState()) {
									case DownloadParameter.STATE_START:
										
										break;
									case DownloadParameter.STATE_STOP:
										
										break;
									case DownloadParameter.STATE_DOWING:
										
										break;
									case DownloadParameter.STATE_COMPLETE:
										// 产品下载成功后添加产品下载记录
										Products pro = (Products) args;
										if (pro != null) {
											// 下载完成后存储包名到本地
											LocalUtils.updateLocalPackname(
													context, pro.getPackName());
										}
										synchronized (taskThread) {
											// 当前任务下载成功之后唤醒线程继续执行下一个任务
											taskThread.notify();
										}
										break;

									default:
										break;
									}
									
								}
							});
//							DLUtil dlu = new DLUtil(filepath, null, pro);
//							dlu.setOnDownLoadListener(new DLUtil.OnDownLoadListener() {
//
//								@Override
//								public void onDone(DownLoadState dlstate,
//										String filepath, Object object) {
//									
//								}
//
//								@Override
//								public void onChange(int filesize,
//										int downfilesize) {
//								}
//							});
//							dlu.start();

							synchronized (taskThread) {
								try {
									// 每发起一个任务之后开始等待
									taskThread.wait();
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						} else {
							taskState = TaskState.WAIT;
							ZYALogUtils.e("task is wait");
							synchronized (taskThread) {
								try {
									// 网络变更后停止下一个任务，等待被唤醒
									taskThread.wait();
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				ZYALogUtils.e("task is ready");
				taskState = TaskState.READY;
			}
		};
		taskThread.start();
	}
}
